package cn.dida.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.dida.model.LgsLogistics;
import cn.dida.model.LgsRelate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dida.dao.LgsDao;
import cn.dida.model.Pageable;
import cn.dida.service.LgsService;

@Service
public class LgsServiceImpl implements LgsService {
    @Autowired
    private LgsDao lgsDao;

	/**
	 * 批次查询
	 * @param companyId
	 * @param batchNnb
	 * @param page
	 * @return
	 */
	@Override
	public Pageable<Map<String,Object>> queryLgsInfoByBatch(Integer companyId, String batchNnb, Integer page) {

		return lgsDao.queryLgsInfoByBatch(companyId,batchNnb,page);
	}
	public List<Map<String,Object>> batchExport(Integer companyId, String batchNub){
		return lgsDao.batchExport(companyId,batchNub);
	}


	/**
	 * 物流查询   如果瓶码（bottleNo）不为空， 根据瓶码获取箱码； 根据箱码获取产品、瓶码、批次号等
	 * @param companyId
	 * @param bottleNo
	 * @param boxNo
	 * @return
	 */
	@Override
	public Map<String, Object> getLogistics(Integer companyId, String bottleNo, String boxNo){

		List<Map<String,Object>> resultList = new ArrayList<>();
		Map<String, Object> map = new HashMap<String, Object>();
		if(boxNo == null || boxNo.equals("")){
			boxNo = lgsDao.getBoxByBottle(bottleNo,companyId);
		}
		List<Map<String,Object>> list = lgsDao.getLogistics(companyId, boxNo);
		List<LgsRelate> bottleNoList = lgsDao.getBottleByBox(boxNo,companyId);
		List<Map<String,Object>> custScanList=lgsDao.getCustScan(companyId, boxNo);  //网点全部扫描记录 2017//11//24修改 bug233
		if(custScanList != null && custScanList.size()>0){
			resultList.addAll(list);
			resultList.addAll(custScanList);
			ListDateSort(resultList);
		}

//		if(custScan!=null && !custScan.isEmpty()){
//			Long custScanTime = (Long) custScan.get("scanTime");
//			if(list != null && list.size()>0){
//				Long littleTime  = list.get(list.size()-1).getScanTime();
//				if(littleTime>=custScanTime){
//					map.put("lastRow",1);
//				}else{
//					for(LgsLogistics lgs:list){
//						if(lgs.getScanTime()<custScanTime){
//							map.put("scanTime",lgs.getScanTime());
//							break;
//						}
//					}
//				}
//			}
//			map.put("custScan",custScan);
//		}

		map.put("list",resultList);
		map.put("bottleNoList",bottleNoList);
		String proName = ""; String batchNub = "";
		if(list!=null && list.size()>0){
			proName = (String) list.get(0).get("proName");
			batchNub =  (String)list.get(0).get("batchNub");
		}


		map.put("proName", proName);
		map.put("batchNo", batchNub);
		map.put("boxNo", boxNo);
		return map;
	}

	//按照时间排序
	public void ListDateSort(List<Map<String, Object>> list){
		Collections.sort(list, new Comparator<Map<String, Object>>(){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				// TODO Auto-generated method stub
				Long dt1 = (Long)o1.get("scanTime") ;
				Long dt2 =(Long)o2.get("scanTime");

				Long com = dt1-dt2;
				if(com>0){
					return -1;
				}else if(com<0){
					return 1;
				}else {
					return 0;
				}
			}
		});
	}


	/**
	 * 根据箱码获取瓶码
	 */
	public List<LgsRelate> getBottleByBox(String bQrcodeId,Integer companyId){
		return lgsDao.getBottleByBox(bQrcodeId,companyId);
	}

	/**
	 * 根据托盘码获取箱码
	 * @param trayQrcode
	 * @param companyId
	 * @return
	 */
	public List<LgsRelate> getBoxByTray(String trayQrcode,Integer companyId){

		List<Integer> qrCompanyIdList = lgsDao.getTrayCompany(trayQrcode,companyId);
		List<LgsRelate> list = new ArrayList<>();
		for(Integer qrCom:qrCompanyIdList){
			List<LgsRelate> li = lgsDao.getBoxByTray(trayQrcode,qrCom,companyId);
			list.addAll(li);
		}
//		return lgsDao.getBoxByTray(trayQrcode,companyId);
		return list;
	}

	/**
	 * 关联管理
	 * @param cTimeS 操作开始时间
	 * @param cTimeE 操作结束时间
	 * @param depot 操作地点id
	 * @param cAdmin 操作人id
	 * @param page
	 * @return
	 */
	@Override
	public Pageable<LgsRelate> getRelateBox(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin,Integer page){

		return lgsDao.getRelateBox(companyId,cTimeS,cTimeE,depot,cAdmin,page);
	}

	/**
	 * 关联管理  托盘关联
	 * @param companyId
	 * @param cTimeS
	 * @param cTimeE
	 * @param depot
	 * @param cAdmin
	 * @param page
	 * @return
	 */
	public Pageable<LgsRelate> getRelateTray(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin,Integer page){
		return lgsDao.getRelateTray(companyId,cTimeS,cTimeE,depot,cAdmin,page);
	}

	@Override
	public List<LgsRelate> relateBoxExport(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin){
		return lgsDao.relateBoxExport(companyId, cTimeS, cTimeE, depot, cAdmin);
	}

	public List<LgsRelate> relateTrayExport(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin){
		return lgsDao.relateTrayExport(companyId, cTimeS, cTimeE, depot, cAdmin);
	}
	
    
}
